﻿using System;
using System.Windows;
using System.Windows.Input;
using MDCAdminTool.Controls;

namespace MDCAdminTool.Commands
{
    public class AddCacheHost : AsyncCommandBindingBase
    {
        public AddCacheHost()
            : base()
        {
            Command = Application.Current.Resources["AddCacheHost"] as ICommand;
        }

		protected override void OnExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			var cluster = e.Parameter as CacheCluster;
			if (cluster == null)
			{
				var @base = e.Parameter as GroupBase;
				if (@base != null)
				{
					cluster = @base.Cluster;
				}
				else
				{
					return;
				}
			}


			NewCacheHostWin win = null;

			Application.Current.Dispatcher.Invoke(
				new Action(
					() =>
						{
							win = new NewCacheHostWin();
							win.ShowDialog();
						}
					));

			if (win.IsReady)
				cluster.AddHost(win.HostName, win.CachePort);

			Application.Current.Dispatcher.Invoke(
				new Action(() => win.Close()));

		}

    	protected override void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
    }
}